//analyze new grandparent counterfactuals

local files `"baseline cfact_noparent cfact_nolock"'

foreach file in `files'{
	import delimited "$dir/Model/simulated_data/simulated_data_`file'_3.csv", clear
	gen v25 = 2
	tempfile blacks
	save `blacks'

	import delimited "$dir/Model/simulated_data/simulated_data_`file'_2.csv", clear
	gen v25 = 1
	append using `blacks'
	save "$temp/simulated_data_`file'_combined", replace
}




//read in and prepare files
local files `"baseline cfact_noparent cfact_nolock"'
foreach file in `files'{
    use "$temp/simulated_data_`file'_combined", clear
	ren v1 uniqid
	ren v2 year
	ren v3 mu
	ren v4 e
	ren v5 m
	ren v6 p
	ren v7 l
	ren v8 age
	ren v9 x
	ren v10 ac
	ren v11 f
	ren v12 lp_type
	ren v13 h
	ren v14 wage
	ren v15 l_prime
	ren v16 sim
	ren v17 age_int
	ren v21 tau
	ren v22 util
	ren v23 util_start
	ren v24 e_s
	ren v25 race
	replace util_start = util_start/41
	replace f = f - 1
	
	ren util util_expost
	ren util_start util
	
	
	gen temp = l if age_int == 22 & e == 1
	replace temp = l if age_int == 25 & e==2
	bys uniqid sim: egen l_start = max(temp)
	//drop temp
	//keep if (age_int == 22 & e == 1 | age_int == 25 & e == 2)
	gen start_away = (l_start!=1)
	//drop if age_int<25	
	
	//various migration indicators
	gen parent = (l == 1)
	gen mig = (l_prime<11)

	//cost of living levels
	replace l = l - 1
	replace l = lp_type if l == 0
	gen coli = 0
	replace coli = 1.276 if l == 1
	replace coli = 1.212 if l == 2
	replace coli = 1.017 if l == 3
	replace coli = 1.002 if l == 4
	replace coli = 1.052 if l == 5
	replace coli = 0.970 if l == 6
	replace coli = 0.999 if l == 7
	replace coli = 1.064 if l == 8
	replace coli = 1.279 if l == 9	
	replace wage = wage/coli //now a real wage!
	replace wage = wage * 2.08 //reporting real wages in thousands of dollars now, since initially normalized by 2k
	replace util = util * 2.08 //same thing for utility
	replace util_expost = util_expost * 2.08 //same thing for ex-post utility

	//get: total experience, total wages and moves, proportion of life spent with parents. Also separate by college education to explore heterogeneity
	collapse (first) start_away mu race tau e lp_type (max) x (sum) wage mig util util_expost f parent, by(sim uniqid)
	
	ds x wage mig util util_expost f parent 
	foreach var in `r(varlist)'{
		ren `var' `var'_`file'
	}	
	save "$temp/lifecycle_collapsed_`file'", replace
}


//get to work
use "$temp/lifecycle_collapsed_baseline", clear
merge 1:1 uniqid sim using "$temp/lifecycle_collapsed_cfact_noparent", keep(match) nogen
merge 1:1 uniqid sim using "$temp/lifecycle_collapsed_cfact_nolock", keep(match) nogen

//compute various effects
local vars `"x wage mig util util_expost f parent"'
foreach var in `vars'{
		gen `var'_diff_baseline = `var'_cfact_noparent - `var'_baseline //diffence from baseline
		gen `var'_diff_nolock = `var'_cfact_noparent - `var'_cfact_nolock //diffence from baseline
}


//tweak utility to be in dollars
replace util_diff_baseline = util_diff_baseline / 0.09
replace util_diff_nolock = util_diff_nolock / 0.09
replace util_expost_diff_baseline = util_expost_diff_baseline / 0.09
replace util_expost_diff_nolock = util_expost_diff_nolock / 0.09


replace util_baseline = util_baseline / 0.09
replace util_expost_baseline = util_expost_baseline / 0.09

su wage_baseline x_baseline mig_baseline parent_baseline f_baseline util_baseline util_expost_baseline

//report summary statistics
capture program drop tableline 
program tableline
	local condition "`1'"
	local cfact "`2'"
	
	qui{
	preserve
		keep if `condition' 
		
		
		//summary stats: wages, x, moves, time with parents, fertility, and WTP
		su wage_diff_`cfact'
		local wage = round(`r(mean)', 0.01)
		
		su x_diff_`cfact'
		local x = round(`r(mean)', 0.01)
		
		su mig_diff_`cfact'
		local mig = round(`r(mean)', 0.01)
		
		su parent_diff_`cfact'
		local parent = round(`r(mean)', 0.01)
		
		su f_diff_`cfact'
		local f = round(`r(mean)', 0.01)
		
		su util_diff_`cfact'
		local util = round(`r(mean)', 0.01)
		
		su util_expost_diff_`cfact'
		local util_expost = round(`r(mean)', 0.01)
		noi di "`wage' & `x' & `mig' & `parent' & `f' & `util' & `util_expost' \\"
	restore
	}
end

cap log close
log using "$output/Table_12"
//produce the lines
local cfacts `"baseline nolock"'
foreach cfact in `cfacts'{
	noi di "Results for `cfact' Scenario:"
	tableline "uniqid>0" "`cfact'" //all
	tableline "tau == 1" "`cfact'" //helpful gpars
	tableline "race == 1" "`cfact'" //whites
	tableline "race == 2" "`cfact'" //blacks
	tableline "e == 1" "`cfact'" //high school
	tableline "e == 2" "`cfact'" //college
	tableline "!start_away" "`cfact'" //start away
	noi di "" 
}
log close











//end of dofile

